package com.example.foobar.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.foobar.business.service.*;
import com.example.foobar.dao.service.impl.DaQrcodesDaoServiceImpl;
import com.example.foobar.entity.DaOfficialAccount;
import com.example.foobar.entity.DaQrcodes;
import com.example.foobar.entity.ImsAccountWechats;
import com.example.foobar.entity.ImsGhostQrcodes;
import io.geekidea.springbootplus.framework.util.LoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分析系统配置文件 服务实现类
 *
 * @author 郑英奔
 * @since 2021-05-31
 */
@Slf4j
@Service
public class DaQrcodesServiceImpl implements DaQrcodesService {

    @Autowired
    DaQrcodesDaoServiceImpl daQrcodesDaoService;

    @Autowired
    ImsGhostQrcodeRecordsService imsGhostQrcodeRecordsService;

    @Autowired
    ImsGhostQrcodesService imsGhostQrcodesService;

    @Autowired
    DaOfficialAccountService daOfficialAccountService;

    @Override
    public DaQrcodes getById(Long id) {
        return null;
    }

    @Override
    public boolean saveDaQrcodes(DaQrcodes daQrcodes) {
        return daQrcodesDaoService.saveDaQrcodes(daQrcodes);
    }

    @Override
    public boolean updateDaQrcodes(DaQrcodes daQrcodes) {
        return daQrcodesDaoService.updateDaQrcodes(daQrcodes);
    }

    @Override
    public boolean deleteDaQrcodes(Long id) {
        return daQrcodesDaoService.deleteDaQrcodes(id);
    }

    @Override
    public void syncQrCode() {
        List<ImsGhostQrcodes> ghostQrcodes = imsGhostQrcodesService.selectList(new QueryWrapper<>());
        List<DaQrcodes> collect = ghostQrcodes.stream().map(this::imsQrcodesToDaQrcodes).collect(Collectors.toList());
        collect.stream().forEach(daQrcodes -> {
            LambdaQueryWrapper<DaQrcodes> wrapper = new LambdaQueryWrapper<>(DaQrcodes.class).eq(DaQrcodes::getSceneId, daQrcodes.getSceneId());
            DaQrcodes one = daQrcodesDaoService.getOne(wrapper);
            if(one == null){
                daQrcodesDaoService.save(daQrcodes);
            }
        });
    }

    @Override
    public List<DaQrcodes> list() {
        List<DaQrcodes> list = daQrcodesDaoService.list();
        if(LoginUtil.getUserId() == 1){
            return list;
        }else{
            return filterDaQrcode(list);
        }
    }

    private List<DaQrcodes>  filterDaQrcode(List<DaQrcodes> list) {
        List<DaQrcodes> result = new ArrayList<>();
        Long userId = LoginUtil.getUserId();
        List<DaOfficialAccount> daOfficialAccounts = daOfficialAccountService.list(userId);
        List<Integer> uniacidList = new ArrayList<>();
        daOfficialAccounts.forEach(daOfficialAccount -> {
            uniacidList.add(daOfficialAccount.getUniacid());
        });
        list.forEach(daQrcodes -> {
            if(uniacidList.contains(daQrcodes.getUniacid())){
                result.add(daQrcodes);
            }
        });
        return result;
    }

    private DaQrcodes imsQrcodesToDaQrcodes(ImsGhostQrcodes imsGhostQrcodes) {
        DaQrcodes daQrcodes = new DaQrcodes();
        BeanUtils.copyProperties(imsGhostQrcodes, daQrcodes);
        return daQrcodes;
    }
}
